package com.itany.shop.servlet;

import com.itany.shop.entity.Order;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.OrderService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/**
 * @author 黄衡熙
 * @version 1.0
 */
@WebServlet("*.order")
public class OrderServlet extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        String path = req.getServletPath();
        switch (path) {
            case "/create.order":
                createOrder(req, resp);
                break;
            case "/findMyOrder.order":
                findMyOrder(req, resp);
                break;
            case "/findDetail.order":
                findDetail(req, resp);
                break;
            default:
                PrintWriter out = resp.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源有误,请检查请求是否正确</h1>");

        }
    }

    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("OrderServlet");
//        获取参数请求
        User user = (User) req.getSession().getAttribute("user");
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        try {
//        调用业务层实现方法
            String no = orderService.createNo(cart, user);
//        把生成的no存到当前作用于session
            req.getSession().setAttribute("no", no);
//        转发
            resp.sendRedirect(req.getContextPath() + "/jsp/success.jsp");
        } catch (UserNotLoginException e) {
            req.setAttribute("registerMsg", e.getMessage());
            req.getRequestDispatcher("/jsp/register.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("registerMsg", "服务器内部异常");
            req.getRequestDispatcher("/jsp/register.jsp").forward(req, resp);
        }
    }

    protected void findMyOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("OrderService");
        User user= (User) req.getSession().getAttribute("user");
        try {
            List<Order> orders = orderService.findMyOrders(user);
            req.setAttribute("orders",orders);
            req.getRequestDispatcher("/jsp/myOrders.jsp").forward(req,resp);
        } catch (UserNotLoginException e) {
            req.setAttribute("msg",e.getMessage());
            req.getRequestDispatcher("/jsp/confirm.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("productMsg","服务器内部异常");
            req.getRequestDispatcher("/findAll.product").forward(req,resp);
        }
    }

    protected void findDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");
        String id = req.getParameter("id");
        try {
            Order order = orderService.findDetail(id);
            req.setAttribute("order",order);
            req.getRequestDispatcher("/jsp/orderDetail.jsp").forward(req,resp);
        } catch (RequestParameterErrorException e) {
            req.setAttribute("orderMsg",e.getMessage());
            req.getRequestDispatcher("/findMyOrders.order").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("orderMsg","服务器内部异常");
            req.getRequestDispatcher("/findMyOrders.order").forward(req,resp);
        }
    }
}
